package com.cyh.core.generator;

import java.util.List;

import javax.validation.constraints.NotNull;

import com.cyh.core.framework.common.utils.FileUtil;
import com.cyh.core.framework.common.utils.ReadTextUtil;

import org.springframework.util.ClassUtils;

/**
 * @Author: yanhang_chen
 * @Date: 2020-09-22 11:54:39
 */
public class RepositoryGenerator {

    public static void generatorRepository(
            @NotNull String packageName,
            @NotNull String entityName,
            @NotNull String entityPackageName,
            @NotNull String keyType,
            @NotNull String outPutFilePath
    ){
        String text= ReadTextUtil.readFileContent(FileUtil.getRealPath( ClassUtils.getDefaultClassLoader().
                getResource("").getPath()+ "/template/template.repository"));
        text = text.replace("ENTITY_PACKAGE_NAME",entityPackageName+"."+entityName);
        text = text.replace("PACKAGE_NAME",packageName);
        text = text.replace("ENTITY_NAME_",entityName);
        text = text.replace("ENTITY_NAME",entityName);
        text = text.replace("KEY_TYPE",keyType);
        FileUtil.writeBytes(text.getBytes(),outPutFilePath +"/"+ entityName + "Repository.java");
    }

    public static void generatorRepository(
            @NotNull List<String> packageNames,
            @NotNull List<String> entityNames,
            @NotNull List<String> entityPackageNames,
            @NotNull List<String> keyTypes,
            @NotNull List<String> outPutFilePaths
    ){
        String text= ReadTextUtil.readFileContent(FileUtil.getRealPath( ClassUtils.getDefaultClassLoader().
                getResource("").getPath()+ "/template/template.repository"));
        for (int i = 0; i < packageNames.size(); i++) {
            generatorRepository(
                    packageNames.get(i), entityNames.get(i),
                    entityPackageNames.get(i), keyTypes.get(i), outPutFilePaths.get(i)
            );
        }
    }

}
